<?php

namespace App\Console\Commands;

use App\Models\Cart;
use App\Services\GraspService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class AddCartCommand extends Command {
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'system:addCart';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = '商品加入购物车';

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct() {
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function handle() {
		$goods = Cart::where('plan_remind', 0)->get();
		foreach ($goods as $good) {
			$isbn = $good->isbn;
			$dzyGood = GraspService::dzyIsbnGood($isbn);
			if (!$dzyGood || empty($dzyGood)) {
				continue;
			}
			if (!isset($dzyGood['data'][0]['book'])) {
				continue;
			}
			$book = $dzyGood['data'][0]['book'];
			$id = $book['id'];
			$price = $book['price'];
			$good->item_id = $id;
			$good->price = $price;
			$good->plan_remind = 1;
			$good->image = $book['images']['origin'];
			$good->save();

			$everyday_add_cart = config('everyday_add_cart');
			$users = explode(',', $everyday_add_cart);
			foreach ($users as $user) {
				$userid = config($user . '_dzy_userid');
				$cookie = config($user . '_dzy_cookie');
				//加入购物车
				$res = GraspService::execAddDzyCart($id, $userid, $cookie);
				Log::info("账号" . $userid . "加入购物车成功", [$id]);
			}
		}
	}
}



